package com.xteam.youin_supplier.fragment;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonSyntaxException;
import com.xteam.youin_supplier.MainActivity;
import com.xteam.youin_supplier.R;
import com.xteam.youin_supplier.adpter.TruckListViewAdapter;
import com.xteam.youin_supplier.config.GlobalData;
import com.xteam.youin_supplier.config.RequestAddr;
import com.xteam.youin_supplier.model.BaseModel;
import com.xteam.youin_supplier.model.TruckList;
import com.xteam.youin_supplier.model.TruckTeamList;
import com.xteam.youin_supplier.util.http.AjaxCallBack;
import com.xteam.youin_supplier.util.loading.Loading;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ViewById;

/**
 * 加油车辆详情
 */
@EFragment(R.layout.fragment_truck_list)
public class TruckListFragment extends BaseFragment {
    @ViewById(R.id.back_btn)
    ImageButton backBtn;

    @ViewById(R.id.toptitle_title)
    TextView topTitle;

    @ViewById(R.id.listview)
    RecyclerView listView;

    @FragmentArg("time")
    String time = "";
    @FragmentArg("datetime")
    String datetime  ="";
    @FragmentArg("gasstation_id")
    int gasstation_id;
    @FragmentArg("team_id")
    int team_id;


    TruckListViewAdapter truckListViewAdapter;
    TruckListFragment truckListFragment;

    @AfterViews
    @Override
    void initView() {
        truckListFragment = this;
        backBtn.setVisibility(View.VISIBLE);
        topTitle.setText(getResources().getString(R.string.truck_list_title));

        listView.setLayoutManager(new LinearLayoutManager(getActivity()));
        loadData();
    }

    private void loadData() {
        if (time.equals("")) {
            requestParams = "supplier_id="+ GlobalData.userInfo.getData().getSupplier_id()
                    + "&team_id=" + team_id
                    + "&time=" + datetime;
            requestUrl = RequestAddr.carListSearch + requestParams;
        }else{
            requestParams = "gasstation_id=" + gasstation_id
                    + "&team_id=" + team_id
                    + "&time=" + (Integer.parseInt(time) + 1);
            requestUrl = RequestAddr.carList + requestParams;
        }
        xtHttp.get(requestUrl, new AjaxCallBack<Object>() {
            @Override
            public void onSuccess(Object object) {
                super.onSuccess(object);
                dealData(object);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    TruckList truckList;
    private void dealData(Object object) {
        try {
            truckList = gson.fromJson(object.toString(), TruckList.class);
        } catch (JsonSyntaxException e) {
            baseModel = gson.fromJson(object.toString(), BaseModel.class);
            Toast.makeText(getActivity(), baseModel.getMsg(), Toast.LENGTH_SHORT).show();
            return;
        }
        truckListViewAdapter = new TruckListViewAdapter(getActivity().getApplicationContext(), truckList);
        listView.setAdapter(truckListViewAdapter);
        truckListViewAdapter.setOnItemClickListener(new TruckListViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                MainActivity.getInstance().switchFragment(truckListFragment, CarOilDetailFragment_.builder()
                .order_id(truckList.getData().get(position).getOrder_id())
                .build());
            }
        });
    }

    @Click({R.id.back_btn, R.id.edit_btn})
    @Override
    void Click(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                MainActivity.getInstance().getSupportFragmentManager().popBackStack();
                break;
        }
    }
}
